<?php

/**
 * 通知公告
 * @author HelloWorld
 * 2019-8-8
 * UTF-8
 */
class NoticeController extends Controller_Base
{

    public function init()
    {
        parent::init();

    }

    //列表
    public function IndexAction()
    {
        $request = $this->getRequest();
        $page = intval($request->getQuery('page', 1));
        $limit = intval($request->getQuery('limit', 10));
        $page = $page ? $page : 1;
        $start = ($page - 1) * $limit;
        $where = [];


        $seacher = array(
            'where' => $where,
            'limit' => $limit,
            'start' => $start,
            'order' => ' `id` DESC'
        );

        $list = Module_InfoBase_Notice::getInstance()->getAllBySearch(array("*"), $seacher, true);
        $total = Module_InfoBase_Notice::getInstance()->getAllBySearch(array("count(*) as num"), array('where' => $where), true);

        $total = $total[0]['num'];
        $page_query = [];
        SmartyAdapter::assign('arrList', $list);

        SmartyAdapter::assign('curPageNum', $page);
        SmartyAdapter::assign('totalnum', $total);

        SmartyAdapter::assign('totalPage', ceil($total / $limit));

        SmartyAdapter::assign('query', $page_query);

        SmartyAdapter::assign('list', $list);
        SmartyAdapter::assign('pagesize', $limit);
        SmartyAdapter::assign('page_query', http_build_query($page_query));
    }

    public function addAction()
    {
        $id = $this->getRequest()->getQuery('id');
        if(!empty($id)){
            $list = Module_InfoBase_Notice::getInstance()->getItemById($id);
            SmartyAdapter::assign('id', $id);
            SmartyAdapter::assign('list', $list);
        }
        SmartyAdapter::$templateName = 'notice/edit';
    }



    //新添加编辑接口
    public function addAndEditAction()
    {
        $id = $this->getRequest()->getPost('id', 0);
        $status = $this->getRequest()->getPost('status', 0);
        $content = $this->getRequest()->getPost('content', '');
        $startTime = $this->getRequest()->getPost('startTime', '');
        $endTime = $this->getRequest()->getPost('endTime', '');
        if (empty($content) || empty($status)) {
            AjaxTool::returnJsonData(4001);
        }
        if(empty($startTime) || empty($endTime)){
            AjaxTool::returnJsonData(4001,'展示时间段不能为空');
        }
        $startTime = strtotime($startTime);
        $endTime = strtotime($endTime);
        if($startTime>$endTime){
            AjaxTool::returnJsonData(4001,'开始时候不能大于结束时间');
        }

        $ts = time();

        if ($id) {
            $data['addTime'] = $ts;
            $data['status'] = $status;
            $data['startTime'] = $startTime;
            $data['endTime'] = $endTime+86400-1;
            $data['content'] = $this->my_addslashes($content);
            $data['id'] = $id;
            $res = Module_InfoBase_Notice::getInstance()->singleUpdate($data, 'id');
        } else {
            $data['addTime'] = $ts;
            $data['status'] = $status;
            $data['startTime'] = $startTime;
            $data['endTime'] = $endTime+86400-1;
            $data['content'] = $this->my_addslashes($content);
            $res = Module_InfoBase_Notice::getInstance()->singleInsert($data);
        }
        if (!$res) {
            AjaxTool::returnJsonData(400);
        }

        AjaxTool::returnJsonData(200);
    }
}

